import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/centre',
    name: 'centre',
    component: () => import('../views/CentreView.vue')
  },
  {
    path: '/work',
    name: 'work',
    component: () => import('../views/WorkView.vue')
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/MineView.vue'),
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("token");
      // 判断是否登录
      if(!token){
        next("/loginmine");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: '/loginmine',
    name: 'loginmine',
    component: () => import('../views/loginmineView.vue'),
     beforeEnter:(to,from,next)=>{
       let token = window.localStorage.getItem("token");
       // 判断是否登录
       //没有token则跳到当前页面
       if(!token){
         next();
        return;
       }else{//否则跳到个人中心
        next("/my")
       }
   }
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/DetailsView.vue')
  },
  {
    path: '/details2',
    name: 'details2',
    component: () => import('../views/DetailsTwoView.vue')
  },
  {
    path: '/commont',
    name: 'commont',
    component: () => import('../views/CommontView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/loginView.vue')
  },
  {
    path: '/composite',
    name: 'composite',
    component: () => import('../views/CompositeView.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/mycommont',
    name: 'mycommont',
    component: () => import('../views/MycommontView.vue')
  },
  {
    path: '/mycollection',
    name: 'mycollection',
    component: () => import('../views/MycollectionView.vue')
  },
  {
    path: '/mylikes',
    name: 'mylikes',
    component: () => import('../views/MylikesView.vue')
  },
  {
    path: '/history',
    name: 'history',
    component: () => import('../views/HistoryView.vue')
  },
]

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err=>err);
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  },
})

export default router
